package com.example.kaoqinmanager.activity;


import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.kaoqinmanager.base.BaseBindingActivity;
import com.example.kaoqinmanager.bean.PaiBanTable;
import com.example.kaoqinmanager.databinding.ActivityPaiBanAddBinding;
import com.example.kaoqinmanager.utils.DateUtils;
import com.example.kaoqinmanager.utils.MyToast;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.datepicker.MaterialStyledDatePickerDialog;

import java.net.DatagramPacket;
import java.util.Calendar;
import java.util.Locale;

public class PaiBanAddActivity extends BaseBindingActivity<ActivityPaiBanAddBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar.setTitle("排班添加");
        actionBar.setDisplayHomeAsUpEnabled(true);

        getBinding().btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String strContent = getBinding().edContent.getText().toString().trim();
                String strStartTime = getBinding().tvStartTime.getText().toString().trim();
                String strEndTime = getBinding().tvEndTime.getText().toString().trim();

                if (TextUtils.isEmpty(strContent)) {
                    MyToast.showToast("内容不能为空");
                    return;
                }
                if (TextUtils.isEmpty(strStartTime)) {
                    MyToast.showToast("开始时间不能为空");
                    return;
                }
                if (TextUtils.isEmpty(strEndTime)) {
                    MyToast.showToast("结束时间不能为空");
                    return;
                }


                PaiBanTable paiBanTable = new PaiBanTable();
                paiBanTable.setName(strContent);
                paiBanTable.setStartTime(strStartTime);
                paiBanTable.setEndTime(strEndTime);
                paiBanTable.save();

                MyToast.showToast("添加成功");
                finish();

            }
        });

        getBinding().tvStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(getBinding().tvStartTime);
            }
        });
        getBinding().tvEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(getBinding().tvEndTime);
            }
        });


    }

    private void showDialog(TextView tv) {
        //获取日期格式器对象

        Calendar calendar = Calendar.getInstance(Locale.CHINA);
       /*  DatePickerDialog datePickerDialog = new DatePickerDialog(PaiBanAddActivity.this, new DatePickerDialog.OnDateSetListener() {

            @Override

            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                //修改日历控件的年，月，日
                //这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致

                calendar.set(Calendar.YEAR, year);

                calendar.set(Calendar.MONTH, month);

                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                //updateTimeShow();

            }

        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

        datePickerDialog.show();*/


        TimePickerDialog pickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                calendar.set(Calendar.HOUR, i);
                calendar.set(Calendar.MINUTE, i1);


                String time = DateUtils.formatDateTime(calendar.getTime(), "HH:mm");
                tv.setText(time);


            }
        }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true);
        pickerDialog.show();


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //actionbar的左侧图标的点击事件处理
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}